package cn.edu.neusoft.model.impl;

import cn.edu.neusoft.model.bean.Car;
import cn.edu.neusoft.model.bean.CarInfo;
import cn.edu.neusoft.model.mapper.CarInfoMapper;
import cn.edu.neusoft.service.car.CarInfoHandler;
import cn.edu.neusoft.service.car.GetAllCarInfoRequest;
import cn.edu.neusoft.service.car.GetAllCarInfoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CarInfoImpl {
    @Autowired
    private CarInfoHandler carInfoHandler;
    @Autowired
    private CarInfoMapper carMapper;
    public void restoreAllCars(){
        int pageNum = 0;
        GetAllCarInfoRequest getAllCarInfoRequest = new GetAllCarInfoRequest();
        getAllCarInfoRequest.setPageSize(100);

        GetAllCarInfoResponse carInfoByPage = null;
        carMapper.truncateCarInfoTable();
        do
        {
            getAllCarInfoRequest.setPageNum(++pageNum);
            carInfoByPage = carInfoHandler.getCarInfoByPage(getAllCarInfoRequest);
            for (Car pageDatum : carInfoByPage.getData().getPageData()) {
                carMapper.insertCarInfo(new CarInfo(pageDatum));
            }

        }while(carInfoByPage != null && carInfoByPage.getData().getCurrentPage() < carInfoByPage.getData().getTotalPage() );
    }
}
